<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_DatabaseManager
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * Static factory class for creating concrete database managers.
 *
 * @package SmartL_DatabaseManager
 */
class SmartL_DatabaseManager {
	/**
	 * Array of instances of user-defined database managers.
	 * Zend_Loader_PluginLoader is not used, because managers should be singletons.
	 *
	 * @var array[numeric]=>SmartL_DatabaseManager_Interface
	 */
	private static $_customManagers = array ();
	
	/**
	 * Static method for adding custom managers in runtime.
	 * Call it in bootstrap or some plugin to add your own manager.
	 *
	 * @param string $name
	 * @param SmartL_DatabaseManager_Interface $instance
	 */
	public static function addCustomManager($name, SmartL_DatabaseManager_Interface $instance) {
		self::$_customManagers [strtolower ( $name )] = $instance;
	}
	
	/**
	 * Factory static method for getting Database managers.
	 *
	 * @param string $name Name of Database manager.
	 * @return SmartL_DatabaseManager_Interface
	 */
	public static function factory($name) {
		$name = strtolower ( $name );
		switch ($name) {
			case "doctrine" :
				{
					require_once 'SmartL/DatabaseManager/Doctrine.php';
					return SmartL_DatabaseManager_Doctrine::getInstance ();
				}
			case "zenddb" :
				{
					require_once 'SmartL/DatabaseManager/ZendDb.php';
					return SmartL_DatabaseManager_ZendDb::getInstance ();
				}
			default :
				{
					if (isset ( self::$_customManagers [$name] )) {
						return self::$_customManagers [$name];
					}
					require_once 'SmartL/DatabaseManager/Exception.php';
					throw new SmartL_DatabaseManager_Exception ( "No DatabaseManager with name {$name}" );
				}
		}
	}
}